package com.bibrApp.difu.fragment.jiedai_mvp;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.bibrApp.difu.Config;
import com.bibrApp.difu.Response.C2CJiedaiResBody;
import com.bibrApp.difu.Response.PaySettingResPonseBody;
import com.bibrApp.difu.Response.TradePawnInfo;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.fragment.wallet_mvp.IWalletView;
import com.bibrApp.difu.utils.DialogUtils;
import com.bibrApp.difu.utils.EncryptUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class JiedaiPresenter extends BasePresenter<IJiedaiView> {

    public JiedaiPresenter(Context context, IJiedaiView iView) {
        super(context, iView);
    }

    /**
     * @pageSize Integer    是	每页条数
     * pageNumber	Integer	是	当前页码
     * type	Integer	是	广告类型；0-借出，1-借入 2-全部
     * status	Integer	是	0-上架、1-下架、2-暂停、3-待交割
     * cycle	Integer	否	周期
     * pawnCoinId	String	否	抵押币币种id
     * sort	Integer	否	0-数量升序、1-数量降序
     * dailyRateSort	Integer	否	0-日利率升序 1-日利率降序
     * incomeSort	Integer	否	0-收益升序 1-收益降序
     * pawnRateSort	Integer	否	0-抵押率升序 1-抵押率降序
     */
    public void getData(int pageSize, int pageNum, int status, int type, String pawnCoinId, String cycle, int sort, int dailyRateSort, int incomeSort, int pawnRateSort) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("pageNumber", pageNum);
        map.put("type", type);
        if (!TextUtils.isEmpty(cycle)) {
            map.put("cycle", Integer.valueOf(cycle));
        }
        map.put("status", status);
        map.put("pawnCoinId", pawnCoinId);
        map.put("sort", sort);
        map.put("dailyRateSort", dailyRateSort);
        map.put("incomeSort", incomeSort);
        map.put("pawnRateSort", pawnRateSort);
        RetrofitNet.getInstance()
                .getApi()
                .getPawnList(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        Log.i("ttt", "--------" + value);
                        String jsonStr = EncryptUtils.objectToJson(value);
                        if (TextUtils.isEmpty(jsonStr)) {
                            return;
                        }
                        C2CJiedaiResBody datas = new Gson().fromJson(jsonStr, C2CJiedaiResBody.class);
                        iView.onDatas(datas);

                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        Log.i("ttt", "--------" + msg);
                    }
                });

    }

    /**
     * 获取抵押币币种信息
     */
    public void getTradeBInfo() {
        RetrofitNet.getInstance()
                .getApi()
                .getTradePawnInfo()
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if (value != null) {
                            String jsonStr = EncryptUtils.objectToJson(value);
                            List<TradePawnInfo> list = new Gson().fromJson(jsonStr, new TypeToken<List<TradePawnInfo>>() {
                            }.getType());
                            if (list == null)
                                return;
                            iView.successOfTradeBInfo(list);
                        }

                    }
                });
    }

    public void getAllPaySetting() {
        RetrofitNet.getInstance()
                .getApi()
                .queryAllPaySetting()
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context, false) {

                    @Override
                    protected void onSuccess(Object value) {
                        if (value != null) {
                            String jsonStr = EncryptUtils.objectToJson(value);
                            if (!TextUtils.isEmpty(jsonStr) && jsonStr.contains("USDT")) {
                                iView.successOfGetPaySetting(true);
                            } else {
                                iView.successOfGetPaySetting(false);
                            }
//                            if (TextUtils.isEmpty(jsonStr)) {
//                                return;
//                            }
//                            List<PaySettingResPonseBody> datas = new Gson().fromJson(jsonStr, new TypeToken<List<PaySettingResPonseBody>>() {
//                            }.getType());
//                            if (datas != null && datas.size() > 0) {
//                                for (int i = 0; i < datas.size(); i++) {
//                                    if (datas.get(i).getCoinEunit().equalsIgnoreCase("usdt")) {
//                                        iView.successOfGetPaySetting(true);
//                                        break;
//                                    }
//                                }
//
//                            }
                        }

                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        iView.successOfGetPaySetting(false);
                    }
                });
    }

    /**
     * 查询总资产
     */
    public void queryAllAssets() {
        RetrofitNet.getInstance()
                .getApi()
                .queryAllAssets()
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if (value != null) {
                            if (Double.parseDouble(String.valueOf(value)) > 0) {
                                iView.successOfGetAllAssets(true);
                                return;
                            }
                        }
                        iView.successOfGetAllAssets(false);
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        iView.successOfGetAllAssets(false);
                    }
                });

    }
}
